from xlrd import open_workbook

from utils.config import Config
from utils.logger import Logger

logger = Logger('ExcelHelper').getlogger()


class ExcelHelper():

    def __init__(self):
        self.config = Config('conf/config2.ini')
        self.bk = self.config.read_value('excel', 'bk')
        self.sheet = self.config.read_value('excel', 'sheet')

    def get_sheet(self):
        bk = open_workbook(self.bk)
        sheet = bk.sheet_by_name(self.sheet)
        return sheet

    def read_cell(self, row_num, col_num):
        sheet = self.get_sheet()
        cell_value = sheet.cell_value(row_num, col_num)
        logger.info(f'单元格值为：{cell_value}')
        return cell_value

    def read_row(self, start_row, start_col=0, end_col=None):
        sheet = self.get_sheet()
        row_values = sheet.row_values(start_row, start_col, end_col)
        logger.info(f'行值为：{row_values}')
        return row_values

    def read_col(self, start_col, start_row=0, end_row=None):
        sheet = self.get_sheet()
        col_values = sheet.col_values(start_col, start_row, end_row)
        logger.info(f'列值为：{col_values}')
        return col_values

    def read_list(self, start_row, end_row, start_col, end_col):
        sheet = self.get_sheet()
        data = []
        for i in range(start_row, end_row):
            row_values = sheet.row_values(i, start_col, end_col)
            data.append(row_values)
        logger.info(f'这堆值为：{data}')
        return data
